package com.lbd.media.controller;

import java.util.Arrays;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.media.bean.Faq;
import com.lbd.media.service.FaqService;
import com.lbd.system.bean.Employee;

/**
 *
 */
@RestController
@RequestMapping("/faq")
public class FaqController extends BaseController {
    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    private FaqService faqService;

    /**
     * 按条件分页查询媒体信息
     *
     * @param page 第几页
     * @param rows 一页多少条
     * @param faq
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(int page, int rows, Faq faq) {
        Page<Faq> faqPage = faqService.findByPageConditionJpql(page, rows, faq);
        faqPage.getContent().stream().forEach(faq1 -> {
            if ("01".equals(faq1.getState())) {//TODO 使用常量或数据字典
                //发布状态下，发布时间=变更时间
                faq1.setPublishTime(faq1.getModifiedDate());
            }
        });
        return new Result(ResultState.SUCCESS, "查询成功", faqPage);
    }

    /**
     * 保存媒体信息
     *
     * @param faq
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(Faq faq, HttpSession session) {
        Employee employee = (Employee) session.getAttribute(Employee.SESSION_USER);
        faq.setCreateUserId(employee.getId());
//        if(StringUtils.isBlank( faq.getPreview())){
//            String previem="";
//            String regx="<.+?>";
//            String content = faq.getContent().replaceAll(regx, "");
//            if(content.length()>100){
//                previem=content.substring(0,100);
//            }else{
//                previem=content;
//            }
//            faq.setPreview(previem);
//        }
        faqService.save(faq);
        return new Result(ResultState.SUCCESS, "添加成功", "");
    }

    /**
     * 删除媒体信息
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestParam("ids[]") String[] ids) {
        System.out.println(Arrays.toString(ids));
        faqService.del(ids);
        return new Result(ResultState.SUCCESS, "删除成功", "");
    }

    /**
     * 根据id查询回显要编辑的媒体信息
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", faqService.findById(id));
    }

    /**
     * 修改媒体信息
     *
     * @param faq
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(Faq faq) {
        faqService.update(faq);
        return new Result(ResultState.SUCCESS, "更新成功");
    }

}
